﻿using System.Xml.Linq;
using PTop.Application.WeChat;
using PTop.Code;
using PTop.Domain.ViewModel.Wechat;

namespace PTop.Front.Areas.Wechat.Models
{
    public class TextMessage : WechatMsgBase
    {
        /// <summary>
        /// 接收消息模版
        /// </summary>
        private static string _template;
        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }
        /// <summary>
        /// 消息ID
        /// </summary>
        public string MsgId { get; set; }


        public static TextMessage LoadFromXml(string xml)
        {
            TextMessage tm = null;
            if (!string.IsNullOrEmpty(xml))
            {
                XElement element = XElement.Parse(xml);
                if (element != null)
                {
                    tm = new TextMessage();
                    tm.FromUserName = element.Element(WeChatCommon.FROM_USERNAME).Value;
                    tm.ToUserName = element.Element(WeChatCommon.TO_USERNAME).Value;
                    tm.CreateTime = element.Element(WeChatCommon.CREATE_TIME).Value;
                    tm.Content = element.Element(WeChatCommon.CONTENT).Value;
                    tm.MsgId = element.Element(WeChatCommon.MSG_ID).Value;
                }
            }

            return tm;
        }

        public override string Template
        {
            get
            {
                if (string.IsNullOrEmpty(_template))
                {
                    _template = @"<xml>
                                <ToUserName><![CDATA[{0}]]></ToUserName>
                                <FromUserName><![CDATA[{1}]]></FromUserName>
                                <CreateTime>{2}</CreateTime>
                                <MsgType><![CDATA[text]]></MsgType>
                                <Content><![CDATA[{3}]]></Content>
                            </xml>";
                }

                return _template;
            }
        }


        public override string HandleRequest()
        {
            var app = new WxAutoReplyApp();
            var reply = app.GetReplyForKeyword(this.Content);
            if (string.IsNullOrEmpty(reply))
            {
                reply = "/微笑您发送的信息我们已收到，我们会尽快处理，感谢您对" + Configs.GetValue("CompanyName") + "的支持。线路咨询、投诉，公交IC卡业务咨询请拨打：0354-8593456；人事招聘请拨打：0354-3260212";
            }
            //普通消息
            var msg = new TextMessage
            {
                Content = reply,
                CreateTime = WeChatCommon.GetNowTime(),
                FromUserName = this.ToUserName,
                ToUserName = this.FromUserName,
            };
            //
            LogFactory.GetLogger("TextMessag").Info("reply:" + reply);

            return msg.GenerateContent();

            //匹配关键字
            //WeChatWapBiz biz = new WeChatWapBiz();
            //var keywordEntity = biz.GetKeyword(this.Content);
            //if (keywordEntity == null)
            //{
            //    var replymsg = "/微笑您发送的信息我们已收到，我们会尽快给您答复，感谢您对搜房网的支持。紧急问题请拨打搜房帮全国咨询热线：400-630-6888";
            //    if (DateTime.Now <= Convert.ToDateTime(ConfigHelper.GetConfig("WeChatAutoReplyDate", "2016-06-08 12:00:00")))
            //    {
            //        replymsg += "\n6月8日上午10点\n互动直播《如何高效使用手机搜房帮》，<a href='http://h.eqxiu.com/s/6df4Z1b4?eqrcode=1'>点此进入</a>";
            //    }
            //    //普通消息
            //    var msg = new TextMessage()
            //    {
            //        Content = replymsg,
            //        CreateTime = WeChatCommon.GetNowTime(),
            //        FromUserName = this.ToUserName,
            //        ToUserName = this.FromUserName,
            //    };
            //    return msg.GenerateContent();
            //}

            ////查找关键字所属规则
            //var ruleEntity = biz.GetRule(keywordEntity.RuleID);

            ////根据规则的回复类型查找对应的回复消息
            //if (ruleEntity.ResMsgType == "text") //返回类型为文字
            //{

            //    var resTexts = biz.GetResText(keywordEntity.RuleID);
            //    if (resTexts.Count <= 0)
            //    {
            //        return "success";
            //    }
            //    var first = resTexts.FirstOrDefault();
            //    var msg = new TextMessage()
            //    {
            //        Content = first.Content,
            //        CreateTime = WeChatCommon.GetNowTime(),
            //        FromUserName = this.ToUserName,
            //        ToUserName = this.FromUserName,
            //    };
            //    return msg.GenerateContent();
            //}
            //else if (ruleEntity.ResMsgType == "news") //返回类型为图文
            //{
            //    var resNews = biz.GetResNews(keywordEntity.RuleID);
            //    if (resNews.Count <= 0)
            //    {
            //        return "success";
            //    }
            //    var items = new List<NewsItemTemp>();
            //    for (int i = 0; i < resNews.Count; i++)
            //    {
            //        var item = new NewsItemTemp()
            //        {
            //            Url = resNews[i].Url,
            //            Title = resNews[i].Title,
            //            PicUrl = resNews[i].PicUrl,
            //            Description = resNews[i].Description
            //        };
            //        items.Add(item);
            //    }
            //    var newsMsg = new NewsMessage(items)
            //    {
            //        FromUserName = this.ToUserName,
            //        ToUserName = this.FromUserName,
            //        CreateTime = WeChatCommon.GetNowTime()
            //    };
            //    return newsMsg.GenerateContent();

            //}
            return "success";
        }
        public string GenerateContent()
        {
            this.CreateTime = WeChatCommon.GetNowTime();
            return string.Format(this.Template, this.ToUserName, this.FromUserName, this.CreateTime, this.Content);
        }

        public override string MsgType
        {
            get { return "text"; }
        }
    }
}